/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import java.util.List;
import javax.persistence.Query;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.Employee;

/**
 *
 * @author HuyNVK
 */
@Stateless
public class EmployeeFacade extends AbstractFacade<Employee> implements EmployeeFacadeRemote {
    @PersistenceContext(unitName = "ejb-servPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EmployeeFacade() {
        super(Employee.class);
    }

    @Override
    public Employee merge(Employee employee) {
         return em.merge(employee);
    }

    
    @Override
    public void deleteByID(int id) {
       Employee employee = find(id);
       employee = em.merge(employee);
       em.remove(employee);
    }

    @Override
    public void insert(Employee employee) {
         em.persist(employee);
    }

    @Override
    public void save(Employee employee) {
        em.merge(employee);
    }
    @Override
    public int selectIDByPassName(String shortname, String password) throws Exception {
        Query query = em.createQuery("Select p.employeeID from Employee p where p.password='" 
                + shortname + "',p.shortname='"+password+"'");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public Employee selectByShortName(String shortname) throws Exception {
        Query query = em.createQuery("Select p from Employee p where p.shortName=:name");
        query.setParameter("name", shortname);
        List<Employee> list = query.getResultList();
        if(list.size() > 0)
            return list.get(0);
        else
            return null;
    }
}
